<?php

	/**
	 * @package Yomm
	 * @author bsuter
	 *
	 */
	Class Yomm_Db_Plugin_Handler Extends Yomm_Db_Plugins {
		
		private $data = null;
		
		public function __construct($table, $fields, $options = false, $id_handler_return = false)
		{
			switch($options)
			{
				// INSERT
				case false:
					// 
					$field_name = null; $field_value = null; $i = 0;
					foreach($fields as $key => $value)
					{
						$i++;
						$field_name .= $key;
						$field_value .= "'$value'";
						if(count($fields)!=$i){
							$field_name .= ',';
							$field_value .= ',';
						}
					}
					$fields = '('.$field_name.') VALUES ('.$field_value.')';
					
					$q = 'INSERT INTO `'.$table.'` '.$fields;
					
				break;
				
				// Update
				case true:
					
					$field = null; $i = 0;
					foreach($fields as $key => $value)
					{
						$i++;
						$field .= $key . '=\'' . $value . '\'';
						if(count($fields)!=$i){
							$field .= ',';
						}
					}
					$fields = $field;
					$q = 'UPDATE `'.$table.'` SET '.$fields.' '.$options;
				break;
			}
			
			$this->data = self::$mysqli->query($q);
			
			if($id_handler_return === true)
			{
				$this->data = self::$mysqli->insert_id;
			}
			
			if(!$this->data)
			{
				throw new Yomm_Error(self::$mysqli->error . " [QUERY: {$q}]");
			}
		}
		
		public function returnValue()
		{
			return $this->data;
		}
	}